﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Diagnostics;
using DBWTProject.persistence;
using DBWTProject.models;
using System.Data;
using System.Globalization;
using System.Data.SqlClient;

namespace DBWTProject.route
{
    public partial class MyRoutes : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            //RouteTable aufbauen TODO: gleich im Master machen (dann auch von UserProfile.aspx raus)
            User user = (User)Session[Constants.SESSIONVAR_CURRENTUSER];
            if (user == null)
            {//nicht eingeloggt
                Response.Redirect("/");
                return;
            }

            if (!Page.IsPostBack)//Nur bei GET
            {
                IList<Route> routeList = RouteDAO.GetRoutes(user);
                grvAllRoutes.DataSource = routeList;
                grvAllRoutes.DataBind();

                dlYear.DataSource = RouteDAO.GetYearsWithRoutes(user);
                dlYear.DataBind();
            }

            //DataTable dt = new DataTable();
            //sqlcmd = new SqlCommand("select * from emp", sqlcon);
            //sqlcon.Open();
            //da = new SqlDataAdapter(sqlcmd);
            //dt.Clear();
            //da.Fill(dt);
            //if (dt.Rows.Count > 0)
            //{
            //    Session["dt"] = dt;
            //    GridView1.DataSource = dt;
            //    GridView1.DataBind();
            //}     
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (fupKMLUpload.HasFile)
            {
                //fupKMLUpload.SaveAs("D:/Temp/upload/"+fupKMLUpload.FileName);
                Stream fileStream = fupKMLUpload.FileContent;
                StreamReader reader = new StreamReader(fileStream);
                string kml = reader.ReadToEnd();
                Debug.WriteLine(kml);

                User user = (User)Session[Constants.SESSIONVAR_CURRENTUSER];

                string routeName = txbRouteName.Text;
                RouteDAO.InsertNewKML(user, routeName, kml);

                Response.Redirect(Request.Url.ToString());
            }
        }

        protected void ibtnFilterDate_Click(object sender, ImageClickEventArgs e)
        {
            User user = (User)Session[Constants.SESSIONVAR_CURRENTUSER];
            IList<Route> routeList;

            string nameFilterText = txbName.Text == "" ? null : txbName.Text;
            DateTime? selectedDateStart = txtDateStart.Text == "" ? null : (DateTime?)DateTime.Parse(txtDateStart.Text, CultureInfo.CreateSpecificCulture("de-DE"));
            DateTime? selectedDateEnd = txtDateEnd.Text == "" ? null : (DateTime?)DateTime.Parse(txtDateEnd.Text, CultureInfo.CreateSpecificCulture("de-DE"));

            routeList = RouteDAO.GetRoutes(user, selectedDateStart, selectedDateEnd, nameFilterText);
            //routeList = RouteManagerDAO.getRoutes(user.ID);
            grvAllRoutes.DataSource = routeList;
            grvAllRoutes.DataBind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            IList<int> routeIdDeathList = new List<int>();
            foreach (GridViewRow row in grvAllRoutes.Rows)
            {
                CheckBox checkBox = (CheckBox)row.FindControl("chbRouteSelector");
                if (checkBox.Checked)
                {
                    routeIdDeathList.Add((int)grvAllRoutes.DataKeys[row.RowIndex]["ID"]);
                }
            }
            if (routeIdDeathList.Count > 0)
            {
                RouteDAO.DeleteRoutes(routeIdDeathList);
                Response.Redirect(Request.Url.ToString());
            }
        }

    }
}